package com.github.fancyideas.exceptions.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@Slf4j
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
        log.error("错误：{}", map);
        map.remove("trace");
        Map<String, Object> detail = (Map<String, Object>) webRequest.getAttribute("detail", RequestAttributes.SCOPE_REQUEST);
        map.put("detail", detail);
        return map;
    }
}
